/*
 *  Licensed to PECATS under one or more contributor license agreements below:
 *  1. http://www.mozilla.org/MPL/MPL-1.1.html
 *  2. http://www.gnu.org/licenses/gpl-2.0.html
 *  3. http://www.gnu.org/licenses/lgpl-2.1.html
*/

/* **********************************************************
*  Servlet for Managing Account Information
*  Name: AccoutManagerServlet.java
*  Author: www.pecats.com
*  Date: Sep 15, 2008
************************************************************/

package com.pecats.server.core;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;

import com.pecats.server.core.CustomerAccountManager;
import com.pecats.server.core.Account;
import com.pecats.server.data.ConfigData;

/**
 * Servlet used to create account details for a customer
 */

public class AccountManagerServlet extends HttpServlet 
{

    RequestDispatcher dispatcher;
    Long accountid ;

    /*
     * Method used to handle incoming http request.
     * @param request HttpServletRequest object from the incoming request
     * @param response HttpServletResponse object which forms the response
    */
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response)
            throws ServletException
	{

		try
		{
	            // Handle actions
			Account account = new Account();	

			account.setBalance(Integer.parseInt(ConfigData.defaultBalance));
			account.setFiles(Integer.parseInt(ConfigData.defaultFiles));
			account.setSize(Integer.parseInt(ConfigData.defaultSize));

			CustomerAccountManager customManager = new CustomerAccountManager();
			accountid = customManager.createAccount(account);			

            	customManager.addCustomerToAccount(Long.parseLong(request.getAttribute("customerid").toString()), accountid);

			request.setAttribute("accountid", Long.toString(accountid));
			request.setAttribute("loginmessage", "Account Sucessfully created, please login to upload a file");
			
			dispatcher = getServletContext().getRequestDispatcher("/LoginJSP.jsp");		
			dispatcher.forward(request, response);

            } 
		catch (Exception ex) 
		{
            	throw new ServletException(ex);
        	}
	}
}